package com.malou.client.ventas.modeloprenda;

import java.util.HashMap;

import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Frame;
import com.malou.client.MalouConstants;
import com.malou.client.reportes.ReporteService;
import com.malou.client.reportes.ReporteServiceAsync;

public class CodigoPrendaDialog extends Dialog{
	private Frame frame;
	private ContentPanel pdfPanel;
	private MalouConstants constants = GWT.create(MalouConstants.class);
	

	public CodigoPrendaDialog(){
		setSize(550, 250);
		setLayout(new FitLayout());
		setButtons("");
		setModal(true);
		pdfPanel = new ContentPanel();
	    pdfPanel.setVisible(true);
	    pdfPanel.setHeaderVisible(false);
	   
	    pdfPanel.setLayout(new FitLayout());	    
	    add(pdfPanel);
	}	

	public void imprimir(Integer modeloprenda_id){
		pdfPanel.removeAll();
		frame = new Frame();	

		pdfPanel.add(frame);
		pdfPanel.layout();		
		AsyncCallback<String> reportURLCallback = new AsyncCallback<String>(){
			
			MessageBox messageBox = new MessageBox();

			@Override
			public void onFailure(Throwable caught) {
				messageBox.setMessage(constants.errorConexion());
				messageBox.show();
				caught.printStackTrace();				
			}

			@Override
			public void onSuccess(String result) {
				if (result != null){		
					result = result.replace("reports/", "");
					frame.setUrl(GWT.getModuleBaseURL() +"DownloadPDF?name="+result);					
				}else{
					messageBox.setMessage(constants.noSePudoCargarReporte());
					messageBox.show();
				}								
			}
			
		};
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("modeloprenda_id", modeloprenda_id);
		((ReporteServiceAsync) GWT.create(ReporteService.class)).createPdf("reports/codigobarra", param, null, modeloprenda_id, reportURLCallback);	
		
	}	
}
